package com.fancy.learncenter.utils;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.fancy.learncenter.R;
import com.fancy.learncenter.common.MAudioPlayer;

/**
 * Created by shiweiwei on 2017/12/20.
 */

public class CartoonToastUtils {

    private static Toast mToast;
    private static Context mContext;
    private static ImageView imageView;

    private static MAudioPlayer truePlayer;
    private static MAudioPlayer falsePlayer;

    public static void initToast(Context context) {
        mContext = context;

        truePlayer = new MAudioPlayer(context, R.raw.cartoon_true);
        falsePlayer = new MAudioPlayer(context, R.raw.cartoon_error);
    }

    /**
     * 显示自定义的Toast
     */
    public static void show(boolean boo) {

        if (mToast == null) {
            mToast = new Toast(mContext);
            View view = LayoutInflater.from(mContext).inflate(R.layout.cartoon_toast_layout, null);

            imageView = (ImageView) view.findViewById(R.id.cartoon_answer_image);
            if (boo) {
                truePlayer.play();
                imageView.setImageResource(R.mipmap.cartoon_right_answer_toast);
            } else {
                falsePlayer.play();
                imageView.setImageResource(R.mipmap.cartoon_wrong_answer_toast);
            }
            mToast.setGravity(Gravity.CENTER, 0, 0);
            mToast.setView(view);
        } else {
            if (boo) {
                truePlayer.play();
                imageView.setImageResource(R.mipmap.cartoon_right_answer_toast);
            } else {
                falsePlayer.play();
                imageView.setImageResource(R.mipmap.cartoon_wrong_answer_toast);
            }
        }
        mToast.show();
    }


    /**
     * 取消Toast
     */
    public static void cancleToast() {
        if (mToast != null) {
            mToast.cancel();
        }
    }
}
